import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { DialogQueue } from '@/utils/dialogQueue'

export const useReplayModeOptionsStore = defineStore('replayModeOptions', () => {
  // 状态
  const dialogQueue = new DialogQueue()
  const elapsedTime = ref(0)
  const maxValue = ref(0)
  const result = ref(null)

  // 计算属性
  const isValid = computed(() => {
    const num = parseInt(elapsedTime.value)
    return !isNaN(num) && isFinite(num) && num >= 0 && num <= maxValue.value
  })

  // 动作
  function setMaxValue() {
    elapsedTime.value = maxValue.value
  }

  function confirm() {
    if (isValid.value) {
      result.value = parseInt(elapsedTime.value)
      hide()
    }
  }

  function show(max) {
    return dialogQueue.show({
      onLoaded: () => {
        elapsedTime.value = 0
        maxValue.value = max
        result.value = null
      },
      onHidden: () => result.value
    })
  }

  function hide() {
    dialogQueue.hide(result.value)
  }

  return {
    // 状态
    isVisible: dialogQueue.isVisible,
    elapsedTime,
    maxValue,
    // 计算属性
    isValid,
    // 动作
    setMaxValue,
    confirm,
    show,
    hide,
    getQueueLength: dialogQueue.getQueueLength.bind(dialogQueue),
    clearQueue: dialogQueue.clearQueue.bind(dialogQueue)
  }
}) 